package com.vma.jifu.backsys.controller;

import com.jfinal.plugin.activerecord.Page;
import com.jfinal.template.ext.directive.Str;
import com.vma.jifu.bizModel.model.Advertisement;
import com.vma.jifu.bizModel.model.TgVedio;
import com.vma.tools.ReturnsUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by kitabi on 2017/6/26.
 */
public class AdController  extends BaseController{

    public void qryAdList(){
        String title = getPara("title");
        String status= getPara("status");

        String start = getPara("start");
        String end = getPara("end");

        int pageNo = getParaToInt("cPage");
        int pageSize = getParaToInt("pSize");

        List<String> param = new ArrayList<String>();
        String condition = "";

        if(title != null && !"".equals(title)){
            condition += "and title like ? ";
            param.add("%"+title+"%");
        }
        if(status != null && !"".equals(status)){
            condition += "and status = ?";
            param.add(status);
        }

        if(start != null && !"".equals(start)){
            condition += "and t.create_time > ? ";
            param.add(start);
        }
        if(end != null && !"".equals(end)){
            condition += "and t.create_time < ? ";
            param.add(end);
        }

        Page<Advertisement> list = Advertisement.dao.paginate(pageNo,pageSize,"select * ","from advertisement where is_delete =0 "+condition
                ,param.toArray());

        renderJson(ReturnForm(list.getList(),list.getTotalRow()));



    }

    public void updateStatus(){
        String adid= getPara("id");
        int status= getParaToInt("status");

        Advertisement advertisement = Advertisement.dao.findById(adid);
        advertisement.setStatus(status).update();

        renderJson(ReturnsUtil.returnSuccess("修改成功"));
    }

    public void deleteAd(){
        String adid= getPara("id");
        Advertisement advertisement = Advertisement.dao.findById(adid);
        advertisement.setIsDelete(1).update();
        renderJson(ReturnsUtil.returnSuccess("修改成功"));
    }

    public void addad(){
        String adid= getPara("id");


        if(adid != null && !"".equals(adid)){
            Advertisement advertisement = Advertisement.dao.findById(adid);
            setAttr("initData",advertisement);
        }



        renderVelocity("/newshop/system/ad/addad.html");
    }

    public void saveAd(){
        String adid= getPara("id");
        String img_url= getPara("image_url");
        int is_jump= getParaToInt("is_jump");
        String content= getPara("content");
        String title= getPara("title");
        String out_url= getPara("out_url");

        if(adid != null && !"".equals(adid)){
            Advertisement advertisement = Advertisement.dao.findById(adid);
            advertisement.setImgUrl(img_url).setIsJump(is_jump).setContent(content).setTitle(title).setOutUrl(out_url).update();
            renderJson(ReturnsUtil.returnSuccess("修改成功"));
        }else{
            Advertisement advertisement = new Advertisement();

            advertisement.setImgUrl(img_url).setIsJump(is_jump).setIsDelete(0).setContent(content).setOutUrl(out_url).setTitle(title).setCreateTime(new Date()).save();
            renderJson(ReturnsUtil.returnSuccess("新增成功"));
        }




    }
}
